/*********************************************************************************
* Copyright (c) zhujinyang.
* All rights reserved.
*
* \file
*
* main
*
* 
*********************************************************************************/
#include "os_ext_api.h"
#include "core_cm33.h"
#include <stdio.h>
#include "pico/stdlib.h"
#include "g_cfg.h"
#include "pico/multicore.h"
#include "pico/rand.h"
#include "hardware/irq.h"
#include "hardware/sync.h"
#include "hardware/timer.h"




void isr_systick(void)
{
    os_sys_tick_handler();
}



void fail_task_entry(void *arg)
{
    volatile INT32U i = 0;
    
    for (; ;) {
        if (++i >= 1000000) {
            i = 0;
            printf("fair in core%d, %d\n", get_core_num(), (INT32S)arg);
        }
    }
}

void real_task_entry(void *arg)
{
    volatile INT32U i = 0;
    
    for (; ;) {
        if (++i >= 100) {
            i = 0;
            printf("real in core%d, %d\n", get_core_num(), (INT32S)arg);
            os_thread_sleep(get_rand_32() % 1000 + 10);
        }
    }
}

void demo1_task_entry(void *arg)
{
    printf("demo1 core%d, exit\n", get_core_num());
}

void demo_task_entry(void *arg)
{
    volatile INT32U i = 0;
    
    for (; ;) {
        if (++i >= 1000) {
            i = 0;
            printf("demo in core%d, %d----\n", get_core_num(), (INT32S)arg);
            os_thread_sleep(get_rand_32() % 1000 + 100);
            os_thread_create(-1, -1, demo1_task_entry, 0, (void *)0, 1, 0);
        }
    }
}

void sem_task_entry(void *arg)
{
    static void *sem = 0;

    if (!sem) {
        sem = os_semaphore_create(0, 1000, 0);
    }
    
    if ((INT32S)arg == 0) {
        for (; ;) {
            os_thread_sleep(1000);
            os_semaphore_release(sem, 1);
        }
    } else {
        for (; ;) {
            os_semaphore_request(sem, 0);
            printf("get sem in core%d\n", get_core_num());
        }
    }
}

void core1_entry(void) 
{
    INT32S i;
    
    NVIC_SetPriority(-1,   0);  //systick
    NVIC_SetPriority(-2,   1);  //pendsv
    SysTick_Config((150000000 / 1000000) * OS_TICK_US);

    printf("core%d_entry\n", get_core_num());

    for (i = 0; i < 3; i++) {
        os_thread_create(-1, -1, demo_task_entry, 0, (void *)(55 + i), 1, 0);
    }
    
    
    os_sched_start();
}



int main(void) 
{
    INT32S i;
    
    os_core_init();
    /* os core must initialize first */
    stdio_init_all();

    printf("-----------reset---------\n");
    
    NVIC_SetPriority(-1,   0);  //systick
    NVIC_SetPriority(-2,   1);  //pendsv
    SysTick_Config((150000000 / 1000000) * OS_TICK_US);
    
    multicore_reset_core1();
    multicore_launch_core1(core1_entry);
    
    for (i = 0; i < 2; i++) {
        os_thread_create(-1, -1, sem_task_entry, 0, (void *)i, 1, 0);
    }

    for (i = -10; i < 0; i++) {
        os_thread_create(-1, -1, fail_task_entry, 0, (void *)i, 1, 0);
    }

    for (i = 0; i < 8; i++) {
        os_thread_create(1, -1, real_task_entry, 0, (void *)i, 1, 0);
    }

    os_sched_start();
}




